import * as jwt from "jsonwebtoken";

// jwt 相关
export const jwtHelper = () => {
  const KEY = "nest_panghu"; // 秘钥
  const expiresIn = 60 * 60 * 12; // 12个小时 或者直接 字符串 12h 这种
  return {
    getToken(data: object) {
      return jwt.sign(data, KEY, { expiresIn });
    },
    verifyToken(token: string) {
      // 使用异步校验 同步 就是不传第三个回调
      return new Promise((resolve, reject) => {
        jwt.verify(token, KEY, (err, decoded) => {
          if (err) {
            reject(err);
          } else {
            resolve(decoded);
          }
        });
      });
    },
  };
};

// 返回文件字节大小
export function formatFileSize(size) {
  size = Number(size);
  if (isNaN(size) || size === 0) return "0 Bytes";
  const k = 1024;
  const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  const i = Math.floor(Math.log(size) / Math.log(k));
  const formattedSize = parseFloat((size / Math.pow(k, i)).toFixed(2));

  return `${formattedSize} ${sizes[i]}`;
}

// 处理分页
export function handlePageing({ pageNum, pageSize }) {
  let options = {};
  if (pageNum && pageNum > 0 && pageSize && pageSize > 0) {
    options["skip"] = (pageNum - 1) * pageSize;
    options["take"] = +pageSize;
  }
  return options;
}
